package com.mrguo.service.impl.basedata.goods;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mrguo.common.dao.MyMapper;
import com.mrguo.common.entity.PageParams;
import com.mrguo.common.entity.Result;
import com.mrguo.common.service.BaseServiceImpl;
import com.mrguo.common.utils.IDUtil;
import com.mrguo.dao.bsd.GoodsTempDetailMapper;
import com.mrguo.dao.bsd.GoodsTempMapper;
import com.mrguo.vo.goods.GoodAndDetailsTempVo;
import com.mrguo.entity.bsd.GoodsTemp;
import com.mrguo.entity.bsd.GoodsTempDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


/**
 * @author 郭成兴
 * @ClassName UnitServiceImpl
 * @Description 商品模板服务层
 * @date 2019/11/149:28 AM
 * @updater 郭成兴
 * @updatedate 2019/11/149:28 AM
 */
@Service
public class GoodsTempServiceImpl extends BaseServiceImpl<GoodsTemp> {

    @Autowired
    private GoodsTempMapper goodsTempMapper;
    @Autowired
    private GoodsTempDetailMapper goodsTempDetailMapper;

    @Override
    public MyMapper<GoodsTemp> getMapper() {
        return goodsTempMapper;
    }

    @Transactional(rollbackFor = Exception.class)
    public Result saveOrUpdateData(GoodAndDetailsTempVo goodAndDetailsTempVo) {
        if (goodAndDetailsTempVo.getTemp().getId() == null) {
            return addData(goodAndDetailsTempVo);
        } else {
            return updateData(goodAndDetailsTempVo);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public Result delData(Long tempId) {
        goodsTempDetailMapper.delByTempId(tempId);
        goodsTempMapper.deleteByPrimaryKey(tempId);
        return Result.ok();
    }

    public Result listPage(PageParams<GoodsTemp> pageParams) {
        Map<String, Object> data = pageParams.getData();
        Page<GoodsTemp> page = pageParams.getPage();
        page.setRecords(goodsTempMapper.listCustom(page, data));
        return Result.ok(page);
    }

    public GoodAndDetailsTempVo getDataById(Long tempId) {
        GoodsTemp goodsTemp = goodsTempMapper.selectByPrimaryKey(tempId);
        List<GoodsTempDetail> goodsTempDetails = goodsTempDetailMapper.selectListByTempId(tempId);
        GoodAndDetailsTempVo goodAndDetailsTempVo = new GoodAndDetailsTempVo();
        goodAndDetailsTempVo.setDetailList(goodsTempDetails);
        goodAndDetailsTempVo.setTemp(goodsTemp);
        return goodAndDetailsTempVo;
    }

    private Result addData(GoodAndDetailsTempVo goodAndDetailsTempVo) {
        Long tempId = IDUtil.getSnowflakeId();
        GoodsTemp temp = goodAndDetailsTempVo.getTemp();
        List<GoodsTempDetail> detailList = goodAndDetailsTempVo.getDetailList();
        temp.setId(tempId);
        for (GoodsTempDetail detail : detailList) {
            detail.setTempId(tempId);
        }
        goodsTempMapper.insertSelective(temp);
        goodsTempDetailMapper.insertList(detailList);
        return Result.ok();
    }


    private Result updateData(GoodAndDetailsTempVo goodAndDetailsTempVo) {
        GoodsTemp temp = goodAndDetailsTempVo.getTemp();
        List<GoodsTempDetail> detailList = goodAndDetailsTempVo.getDetailList();
        for (GoodsTempDetail detail : detailList) {
            detail.setTempId(temp.getId());
        }
        goodsTempMapper.updateByPrimaryKeySelective(temp);
        goodsTempDetailMapper.delByTempId(temp.getId());
        goodsTempDetailMapper.insertList(detailList);
        return Result.ok();
    }
}
